/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.chinapnr.config;

import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.commons.util.PropertiesUtil;
import org.grayrabbit.pay.core.model.enums.PayResultEnum;
import org.grayrabbit.pay.core.model.exception.PayException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 配置
 * 
 * @author wy
 * @version $Id: ChinapnrConfigUtil.java, v 0.1 2012-10-4 上午11:23:28 wy Exp $
 */
public class ChinapnrConfigUtil {

    private static final Logger LOG          = LoggerFactory.getLogger(ChinapnrConfigUtil.class);

    private static ChinapnrInfo chinapnrInfo = null;

    /**
     * 重置配置信息
     * 
     * @param chinaPayConfig
     */
    public static void reset(ChinapnrInfo chinapnrInfo) {
        ChinapnrConfigUtil.chinapnrInfo = chinapnrInfo;
    }

    /**
     * 初始化
     * 
     * @param chinaPayInfo
     */
    public static void init() {

        ResourceBundle resources = PropertiesUtil.getResourceBundle("config.chinapnr.chinapnr");
        if (resources == null) {
            LOG.error("找不到对应的汇付配置信息..");
            throw new PayException(PayResultEnum.CHINAPNR_NOT_INIT);
        } else {
            ChinapnrInfo chinapnrInfo = new ChinapnrInfo();
            String memId = resources.getString("memId");
            if (StringUtils.isBlank(memId)) {
                throw new PayException("汇付配置错误,memId为空");
            }
            String tradeUrl = resources.getString("tradeUrl");
            if (StringUtils.isBlank(tradeUrl)) {
                throw new PayException("汇付配置错误,tradeUrl为空");
            }
            chinapnrInfo.setMemId(memId);
            chinapnrInfo.setTradeUrl(tradeUrl);
            reset(chinapnrInfo);
        }
    }

    /**
     * 获取配置的汇付信息
     * 
     * @return
     */
    public static ChinapnrInfo getChinapnrInfo() {
        if (chinapnrInfo == null) {
            init();
        }
        return chinapnrInfo;
    }
}
